<template>
  <div>
    <div class="g-bd">
      <div class="g-wrap">
        <div class="pgsrch">
          <input type="text" class="srch" v-model="keyword"
                 @blur="handleBlur"
                 @focus="handleFocus"
                 @keyup.enter="handleEnter"
          >
          <span class="j-flag"></span>
          <a href="javascript:;" title="搜索" class="btn" @click.prevent="handleEnter">搜索</a>
          <div class="u-lstlay" v-show="isShow">
            <div class="m-schlist">
              <p class="note">
                <router-link :to="{name:'SearchView',query:{word:keyword,type:'1002'}}" class="xtag"
                             v-if="keyword.length<=50">搜“{{ keyword }}”相关用户>
                </router-link>
                <router-link :to="{name:'SearchView',query:{word:keyword,type:'1002'}}" class="xtag" v-else>
                  搜“{{ keyword.slice(0,51) + "..." }}”相关用户>
                </router-link>
              </p>
              <div class="rap">
                <div class="itm" v-if="searchList.songs">
                  <h3 class="hd">
                    <i class="icon-1"></i>
                    <em class="f-f1">单曲</em>
                  </h3>
                  <ul class="f-cb">
                    <li v-for="(item,index) in searchList.songs" :key="index">
                      <router-link :to="{name:'SearchView',query:{word:keyword,type:'1'}}" class="s-c0">
                        <!--                        <span class="s-fc7">U</span>-->
                        {{ item.name }} - {{ item.artists[0].name }}
                      </router-link>
                    </li>
                  </ul>
                </div>
                <div class="itm" v-if="searchList.artists">
                  <h3 class="hd">
                    <i class="icon-2"></i>
                    <em class="f-f1">歌手</em>
                  </h3>
                  <ul class="f-cb">
                    <li v-for="(singer,i) in searchList.artists" :key="i">
                      <router-link :to="{name:'SearchView',query:{word:keyword,type:'100'}}" class="s-c0">
                        <!--                        <span class="s-fc7">U</span>-->
                        {{ singer.name }}
                      </router-link>

                    </li>
                  </ul>
                </div>
                <div class="itm" v-if="searchList.albums">
                  <h3 class="hd">
                    <i class="icon-3"></i>
                    <em class="f-f1">专辑</em>
                  </h3>
                  <ul class="f-cb">
                    <li v-for="(album,a) in searchList.albums" :key="a">
                      <router-link :to="{name:'SearchView',query:{word:keyword,type:'10'}}" class="s-c0">
                        <!--                        <span class="s-fc7">U</span>-->
                        {{ album.name }} - {{ album.artist.name }}
                      </router-link>

                    </li>
                  </ul>
                </div>
                <div class="itm" v-if="searchList.playlists">
                  <h3 class="hd">
                    <i class="icon-4"></i>
                    <em class="f-f1">歌单</em>
                  </h3>
                  <ul class="f-cb">
                    <li v-for="(play,p) in searchList.playlists" :key="p">
                      <router-link :to="{name:'SearchView',query:{word:keyword,type:'1000'}}" class="s-c0">
                        <!--                        <span class="s-fc7">U</span>-->
                        {{ play.name }}
                      </router-link>

                    </li>
                  </ul>
                </div>
              </div>
            </div>
          </div>
        </div>
        <div class="m-search">
          <div class="snote">
            搜索“{{ word }}”，找到
            <em class="s-fc6">20</em>
            个用户
          </div>
          <ul class="m-tabs">
            <li>
              <router-link :to="{name:'SearchView',query:{word:word,type:'1'}}" :class="type=='1'?'z-slt':''">
                <em>单曲</em>
              </router-link>
            </li>
            <li>
              <router-link :to="{name:'SearchView',query:{word:word,type:'100'}}" :class="type=='100'?'z-slt':''">
                <em>歌手</em>
              </router-link>
            </li>
            <li>
              <router-link :to="{name:'SearchView',query:{word:word,type:'10'}}" :class="type=='10'?'z-slt':''">
                <em>专辑</em>
              </router-link>
            </li>
            <li>
              <router-link :to="{name:'SearchView',query:{word:word,type:'1014'}}" :class="type=='1014'?'z-slt':''">
                <em>视频</em>
              </router-link>
            </li>
            <li>
              <router-link :to="{name:'SearchView',query:{word:word,type:'1006'}}" :class="type=='1006'?'z-slt':''">
                <em>歌词</em>
              </router-link>
            </li>
            <li>
              <router-link :to="{name:'SearchView',query:{word:word,type:'1000'}}" :class="type=='1000'?'z-slt':''">
                <em>歌单</em>
              </router-link>
            </li>
            <li>
              <router-link :to="{name:'SearchView',query:{word:word,type:'1009'}}" :class="type=='1009'?'z-slt':''">
                <em>声音主播</em>
              </router-link>
            </li>
            <li>
              <router-link :to="{name:'SearchView',query:{word:word,type:'1002'}}" :class="type=='1002'?'z-slt':''">
                <em>用户</em>
              </router-link>
            </li>
          </ul>
          <router-view></router-view>
        </div>

      </div>
    </div>

  </div>
</template>

<script>
export default {
  name: "index",
  data() {
    return {
      keyword: '',
      isShow: false,
      searchList:[]
    }
  },
  methods: {
    handleEnter(){
      if (this.keyword!==''){
        this.$router.push({name:'SearchView',query:{word:this.keyword,type:'1'}})
        this.isShow = false
      }
    },
    handleBlur() {
      setTimeout(() => {
        this.isShow = false
      }, 150)
    },
    handleFocus() {
      setTimeout(() => {
        if (this.keyword) {
          this.isShow = true
        }
      }, 150)
    },
    async search(keyword) {
      let result = await this.$API.reqSearchSuggest(keyword)
      this.searchList = result.data.result
    }
  },

  watch: {
    keyword:{
      handler(newVal){
        if (newVal) {
          this.isShow = true
          this.search(newVal)
        } else {
          this.isShow = false
        }
      },
      immediate:false
    },
    type(newVal) {
      this.type = newVal
    }
  },
  computed: {
    word() {
      return this.$route.query.word
    },
    type() {
      return this.$route.query.type
    }
  },
  mounted() {
    this.keyword = this.$route.query.word
  },
  created() {
    this.$watch('keyword',()=>{
      this.isShow = false
    })
  }
}
</script>

<style scoped lang="less">
.g-bd {
  width: 980px;
  min-height: 700px;
  _height: 700px;
  margin: 0 auto;
  background-color: #fff;
  border: 1px solid #d3d3d3;
  border-width: 0 1px;

  .g-wrap {
    padding: 40px;

    .pgsrch {
      width: 420px;
      height: 40px;
      margin: 0 auto;
      background-position: 0 0;
      z-index: 10;
      position: relative;
      zoom: 1;
      background-image: url("../../assets/sprite.png");

      .srch {
        float: left;
        width: 320px;
        height: 17px;
        margin: 12px 0 0 20px;
        padding: 0;
        background: #fff;
        border: none;
        opacity: 1;
        font-size: 12px;
        color: #333;
        font-family: Arial, Helvetica, sans-serif;
        -webkit-text-size-adjust: none;
      }

      .j-flag {

      }

      .btn {
        cursor: pointer;
        float: right;
        width: 50px;
        height: 40px;
        text-indent: -9999px;
        background: url("../../assets/sprite.png") no-repeat 0 9999px;
        &:hover {
          background-position: -430px 0;
          text-decoration: none;
        }
      }

      .u-lstlay {
        clear: both;
        top: 43px;
        width: 418px;
        position: absolute;
        z-index: 1000;
        left: 0;
        box-sizing: border-box;
        border: 1px solid #bebebe;
        border-radius: 4px;
        background: #fff;
        box-shadow: 0 4px 7px #555;
        text-shadow: 0 1px 0 rgb(255 255 255 / 90%);

        .m-schlist {
          .note {
            padding: 11px 10px;
            border-bottom: 1px solid #e2e2e2;
            color: #666;

            .xtag {
              color: #666;
              //max-width: 60%;
              display: inline-block;
              //text-overflow: ellipsis;
              //white-space: nowrap;
              //overflow: hidden;
            }
          }

          .rap {

            .itm {
              height: auto;
              overflow: hidden;

              .hd {
                float: left;
                width: 52px;
                margin-right: -100px;
                padding: 10px 0 0 10px;
                *padding-top: 9px;
                line-height: 17px;
                *line-height: 19px;
                font-weight: normal;

                i {
                  float: left;
                  margin: 0px 4px 0 0;
                  display: inline-block;
                  overflow: hidden;
                  vertical-align: middle;
                  width: 14px;
                  height: 15px;
                  background-image: url("../../assets/icon.png");
                }

                .icon-1 {

                  background-position: -35px -300px;
                }

                .icon-2 {
                  background-position: -50px -300px;
                }

                .icon-3 {
                  background-position: -35px -320px;
                }

                .icon-4 {
                  background-position: -50px -320px;
                }

                .f-f1 {
                  float: left;
                }
              }

              .f-cb {
                //background: #f9f8f8;
                margin-left: 62px;
                margin-top: -1px;
                padding: 6px 0 5px;
                height: auto;
                overflow: hidden;
                border-top: 1px solid #e2e2e2;
                border-left: 1px solid #e2e2e2;

                li {
                  width: 100%;
                  float: left;

                  .s-c0 {
                    display: block;
                    width: 100%;
                    text-indent: 12px;
                    line-height: 24px;
                    color: #000;
                    text-decoration: none;
                    max-width: 95%;
                    white-space: nowrap;
                    text-overflow: ellipsis;
                    overflow: hidden;

                    &:hover {
                      background: #E3E5E7;
                    }

                    .s-fc7 {
                      color: #0c73c2;
                      text-decoration: none;
                    }
                  }
                }
              }
            }
          }
        }
      }
    }

    .m-search {
      .m-tabs, .m-tabs a, .m-tabs em, .m-tabs1 a, .m-tabs1 em {
        background: url(../../assets/tab.png) no-repeat 0 9999px;
      }

      .snote {
        margin: 28px 0 17px;
        color: #999;

        .s-fc6 {
          color: #c20c0c;
        }
      }

      .m-tabs {
        margin-bottom: auto;
        height: 39px;
        font-size: 14px;
        border: 1px solid #ccc;
        border-width: 0 1px;
        background-position: 0 0;
        background-repeat: repeat-x;
        background-image: url("../../assets/tab.png");

        li {
          position: relative;
          left: -1px;
          float: left;
          height: 39px;
          font-size: 14px;

          .z-slt {
            background-position: left -90px;

            em {
              background-position: right -90px;
            }
          }

          a {
            padding-left: 2px;
            float: left;
            height: 39px;
            font-size: 14px;

            em {
              width: 108px;
              height: 37px;
              padding: 2px 2px 0 0;
              line-height: 37px;
              cursor: pointer;
              text-align: center;
              float: left;
              font-size: 14px;
              color: #333;
              background: url("../../assets/tab.png") no-repeat 0 9999px;

            }
          }
        }
      }
    }
  }
}
</style>